package com.wuhanins.insmytestdemo.app.home;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import com.loper7.date_time_picker.dialog.CardDatePickerDialog;
import com.wuhanins.insmytestdemo.R;
import com.wuhanins.insmytestdemo.databinding.FragmentHomeBinding;
import com.wuhanins.insmytestdemo.domainService.provider.utils.SharedPreferenceUtil;
import com.wuhanins.widget.recy.ComAdapterFactory;
import com.wuhanins.widget.recy.CommonRcyAdapter;
import com.wuhanins.widget.recy.IRcyHolderFactory;
import com.wuhanins.widget.recy.base.OnRecyclerItemClickListen2;
import com.wuhanins.widget.recy.event.RcyBaseViewHolder;
import com.wuhanins.widget.recy.event.RcyHolderViewEventMng;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

import kotlin.Unit;
import kotlin.jvm.functions.Function0;
import kotlin.jvm.functions.Function1;

public class HomeFragment extends Fragment {

    private HomeViewModel homeViewModel;
    private FragmentHomeBinding binding;
    private CommonRcyAdapter mAdapter;
    private ArrayList<ContactsViewModel> models;

    @SuppressLint("MissingPermission")
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        homeViewModel = new ViewModelProvider(this).get(HomeViewModel.class);

        binding = FragmentHomeBinding.inflate(inflater, container, false);
        View root = binding.getRoot();

        final TextView textView = binding.textHome;
        // 获取TelephonyManager实例

        String uuid = SharedPreferenceUtil.getString(requireContext(), "UUID", "");
        homeViewModel.setText(uuid);
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

//                double distance2 = MapUtil.getDistance2(114.36177, 30.54261, 114.36162, 30.54279);
//                INSLog.e("distance2", distance2 + "");
            }
        });
        homeViewModel.getText().observe(getViewLifecycleOwner(), new Observer<String>() {
            @Override
            public void onChanged(@Nullable String s) {
                textView.setText(s);
            }
        });

        models = new ArrayList<>();
        mAdapter = ComAdapterFactory.newInstance()
                .InjectRecyclerView(requireActivity(), models, binding.rcy,
                        new IRcyHolderFactory() {
                            @Override
                            public RcyBaseViewHolder create(ViewGroup parent, int viewType) {
                                return ContactsViewHolder.getInstance(parent);
                            }
                        });
        mAdapter.setOnRecyclerItemClickListen2(new OnRecyclerItemClickListen2() {
            @Override
            public void onRecyclerItemClick(int position, View v, int status) {
                if (status == 1) {
//                    Intent intent = new Intent();
//                    intent.setAction(Intent.ACTION_DIAL);
//                    intent.setData(Uri.parse("tel:" + models.get(position).getPhoto()));
//                    startActivity(intent);
                    setDate();
                }
            }
        });

        PhoneUtil phoneUti = new PhoneUtil(requireContext());
        List<ContactsViewModel> phone = phoneUti.getPhone();
        for (ContactsViewModel model : phone) {
            ContactsViewModel contactsViewModel = new ContactsViewModel();
            contactsViewModel.setPhoto(model.getPhoto());
            contactsViewModel.setName(model.getName());
            models.add(contactsViewModel);
        }
        if (models.size() == 0) {
            ContactsViewModel contactsViewModel = new ContactsViewModel();
            contactsViewModel.setPhoto("95110");
            contactsViewModel.setName("防诈中心客服电话");
            models.add(contactsViewModel);
        }
        models.sort(new Comparator<ContactsViewModel>() {
            @Override
            public int compare(ContactsViewModel o1, ContactsViewModel o2) {
                return o1.getName().compareTo(o2.getName());
            }
        });
        mAdapter.notifyDataSetChanged();

        String var = "*生产单位";
        String var1 = "生产单位";
        boolean contains = var1.contains(var);
        return root;
    }

    private void setDate() {
        CardDatePickerDialog.Builder builder = new CardDatePickerDialog.Builder(requireContext());
        builder
                .setTitle("CARD DATE PICKER DIALOG")
                .setBackGroundModel(0)
                .showBackNow(true)
                .showDateLabel(true)
                .showFocusDateInfo(true)
                .setLabelText("年","月","日","时","分","")
                .setOnChoose("选择", new Function1<Long, Unit>() {
                    @Override
                    public Unit invoke(Long aLong) {
                        return null;
                    }
                })
                .setOnCancel("关闭", new Function0<Unit>() {
                    @Override
                    public Unit invoke() {
                        return null;
                    }
                }).build().show();
    }

    public static class ContactsViewHolder extends RcyBaseViewHolder<ContactsViewModel> implements RcyHolderViewEventMng.RegisterItemClickEvent {
        protected TextView tvName;
        protected TextView tvPhone;

        public static ContactsViewHolder getInstance(ViewGroup parent) {
            View v = LayoutInflater.from(parent.getContext()).inflate(
                    R.layout.item_contacts_manage,
                    parent, false);
            return new ContactsViewHolder(v);
        }

        public ContactsViewHolder(View itemView) {
            super(itemView);
            tvName = $(itemView, R.id.tvName);
            tvPhone = $(itemView, R.id.tvPhone);
        }

        @SuppressLint("SetTextI18n")
        @Override
        public void bindData(ContactsViewModel contactsViewModel) {
            tvName.setText("姓名:" + contactsViewModel.getName());
            tvPhone.setText(" 电话:" + contactsViewModel.getPhoto());
            tvPhone.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mL != null) {
                        mL.onRecyclerItemClick(getCurrentPosition(), itemView, 1);
                    }
                }
            });
        }

        OnRecyclerItemClickListen2 mL;

        @Override
        public void onRegisterClickEvent(OnRecyclerItemClickListen2 l) {
            mL = l;
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }
}